package com.cjk.ssm.controller;

import com.cjk.ssm.entity.Department;
import com.cjk.ssm.entity.Employee;
import com.cjk.ssm.exp.MyDeleteFailException;
import com.cjk.ssm.exp.MyNoDataException;
import com.cjk.ssm.exp.MyUpdFailException;
import com.cjk.ssm.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 员工 -- 控制层
 */
@Slf4j
@Controller
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = {"", "/", "/index"})
    public String findAllPage(@RequestParam(value = "pageNum",required = false) Integer pageNume, Model model){
        PageInfo<Employee> pageInfo;
        try {
            pageInfo= employeeService.findAllPage(pageNume, 2);
            System.out.println(pageInfo);
            model.addAttribute("page", pageInfo);

        } catch (MyNoDataException e){
            model.addAttribute("msg", "暂无数据");
        } catch (Exception e) {
            model.addAttribute("msg", "系统繁忙");
        }
        return "employee_index";
    }

    /**
     * 新增员工
     * @return
     */
    @RequestMapping("/addpage")
    public String addpage(){
        return "employee_add_page";
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(Employee employee,Model model){
        System.out.println(employee);
        try {
            employeeService.add(employee);
        }catch (MyNoDataException e){
            //保存失败，跳转到失败提示页面
            model.addAttribute("msg","亲,保存失败~");
            return "err_page";
        }
        catch (Exception e) {
            //其他异常情况，跳转到异常提示页面s
            model.addAttribute("msg","亲，系统繁忙~");
            return "err_page";
        }
        //重定向
        return "redirect:/emp";
    }

    /**
     * 删除员工
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/del")
    public String del(@RequestParam(value ="id",required = true) Integer id,Model model){
        try {
            System.out.println(id);
            employeeService.del(id);
            return "redirect:/emp";//删除成功返回首页
        } catch (MyDeleteFailException e) {
            model.addAttribute("msg","删除失败");
            return "err_page";
        }catch (Exception e){
            model.addAttribute("msg","系统繁忙");
            return "err_page";
        }
    }
    @RequestMapping("/toUpdpage/{id}")
    public String upd(@PathVariable("id")Integer id,Model model){
        Employee employee;
        try {
          employee= employeeService.findById(id);
        } catch (MyNoDataException e) {
            model.addAttribute("msg","暂无数据");
            return "err_page";
        }catch (Exception e){
            model.addAttribute("msg","系统出错了！");
            return "err_page";
        }
        model.addAttribute("employee",employee);
        return "employee_upd_page";
    }
    @RequestMapping(value = "/upd",method = RequestMethod.POST)
    public String updpage(Employee employee,Model model){
        try {
            employeeService.upd(employee);
        } catch (MyUpdFailException e) {
            model.addAttribute("msg","修改失败");
            return "err_page";
        }catch (Exception e){
            model.addAttribute("msg","系统繁忙！");
            return "err_page";
        }
        return "redirect:/emp";
    }

    @RequestMapping(value = "/findbyname",method = RequestMethod.GET)
    public String findByEmpNameLikePage(@RequestParam(value = "name") String name,
                                         @RequestParam(value = "pageNum",required = false) Integer pageNume,
                                         Model model){
        try {
            PageInfo<Employee> pageInfo = employeeService.findByEmpNameLikePage(name, pageNume, 2);
            System.out.println(pageInfo);
            model.addAttribute("name",name);
            model.addAttribute("page",pageInfo);
        } catch (MyNoDataException e) {
            model.addAttribute("msg","暂无数据");
        }catch (Exception e){
            model.addAttribute("msg","系统繁忙");
        }
        return "employee_emp_index";
    }
}
